<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\LinksRequest;
use App\Http\Controllers\Controller;
use App\Model\Links;
use DB,Session,Input;

class LinksController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 获取列表
        $linksList = Links::getLinksList();
        
        return view('admin.links.index', compact('linksList'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.links.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(LinksRequest $request)
    {
        $data = $request->except('_token');
        $data['link_status'] = 1;
        
        $res = Links::insert($data);
        if(false !== $res){
            return redirect('/admin/links');
        } else {
            return back()->with(['error'=>'添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Links::where('id', $id)
                ->where('link_status', '<>', -1)
                ->first();
        return view('admin.links.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(LinksRequest $request, $id)
    {
        $data = $request->except('_token','_method');
        
        $res = Links::where('id',$id)->update($data);
        if(false !== $res){
            return redirect('/admin/links');
        } else {
            return back()->with(['error'=>'修改失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id=0)
    {
        $links = new Links();
        $data = ['link_status'=>-1];
        if($id == 0){
            $id = Input::get('id');
        }
        if(is_array($id)){
            $action = '批量';
            $res = $links->whereIn('id', $id)->update($data);
        } else {
            $action = '';
            $res = $links->where('id', $id)->update($data);
        }
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>$action . '删除成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>$action . '删除失败']);
        }
    }
    
    // 审核
    public function isopen()
    {
        $id = Input::get('id');
        $status = Input::get('status');
        
        $links = new Links();
        $data = ['link_status'=>$status];
        
        if($status == 1){
            $action = '审核';
        } else {
            $action = '隐藏';
        }
        
        if(is_array($id)){
            $action = '批量' . $action;
            $res = $links->whereIn('id', $id)->update($data);
        } else {
            $res = $links->where('id', $id)->update($data);
        }
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>$action . '成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>$action . '失败']);
        }
    }
    
    // 修改排序
    public function changeOrder(Request $request)
    {
        $id = $request->get('id');
        $sort = $request->get('sort');
        $res = Links::where('id', $id)->update(['link_sort'=>$sort]);
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>'修改排序成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>'修改排序失败']);
        }
    }
}
